package com.myapp.mobilesafe.activity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.myapp.mobilesafe.utils.PrefUtils;

public class SetupDirect03Activity extends BaseSetupActivity {
    private EditText etPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_direct03);
        etPhone = (EditText) findViewById(R.id.et_phone);
        String phone = PrefUtils.getString("save_phone", "", this);
        etPhone.setText(phone);
    }

    @Override
    public void goBack() {
        startActivity(new Intent(this, SetupDirect02Activity.class));
        finish();
        overridePendingTransition(R.anim.anim_back_in, R.anim.anim_back_out);
    }

    @Override
    public void goNext() {
        String phone = etPhone.getText().toString().trim();
        if (!TextUtils.isEmpty(phone)) {
            PrefUtils.putString("save_phone", phone, this);
            startActivity(new Intent(this, SetupDirect04Activity.class));
            finish();
            overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
        } else {
            Toast.makeText(this, "请输入安全号码", Toast.LENGTH_SHORT).show();
        }

    }

    public void selectcontact(View view) {
        //startActivityForResult(new Intent(this, ContactActivity.class), 0);
        Uri uri = Uri.parse("content://contacts/people");
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivityForResult(intent, 0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("hahah", "@!#!@#!@#!@#!@#");
        if (requestCode == 0 && resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cursor = managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();
            String number = this.getContactPhone(cursor);
            number = number.replaceAll("-", "").replaceAll(" ", "");
            Log.i("hahah", number + "!!!!!!@@@!!!!");
            etPhone.setText(number);
        }
    }
    private String getContactPhone(Cursor cursor) {
        // TODO Auto-generated method stub
        int phoneColumn = cursor
                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int phoneNum = cursor.getInt(phoneColumn);
        String result = "";
        if (phoneNum > 0) {
            // 获得联系人的ID号
            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            String contactId = cursor.getString(idColumn);
            // 获得联系人电话的cursor
            Cursor phone = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
                            + contactId, null, null);
            if (phone.moveToFirst()) {
                for (; !phone.isAfterLast(); phone.moveToNext()) {
                    int index = phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int typeindex = phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                    int phone_type = phone.getInt(typeindex);
                    String phoneNumber = phone.getString(index);
                    result = phoneNumber;
//					switch (phone_type) {//此处请看下方注释
//					case 2:
//						result = phoneNumber;
//						break;
//
//					default:
//						break;
//					}
                }
                if (!phone.isClosed()) {
                    phone.close();
                }
            }
        }
        return result;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
